package com.sgb.kjwl.view.widget.pick;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.AdapterContentBinding;
import com.sgb.kjwl.model.entity.pick.UserInquiryForTender;


public class PickLetterListView extends RelativeLayout {

    protected Context mContext;
    protected AdapterContentBinding viewBinding;
    public ItemChildCLickListener itemChildCLickListener;

    public PickLetterListView(Context context) {
        this(context, null);
    }

    public PickLetterListView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PickLetterListView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public PickLetterListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initView();
    }

    private void initView() {
        viewBinding = DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.adapter_content, this, true);
        viewBinding.etTitle.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String str = s.toString();
                if (str.length() > 10) {
                    str = str.substring(0, 10);
                    viewBinding.etTitle.setText(str);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (editable.length() > 10) {
                    editable.delete(10, editable.length());
                }
            }
        });
        viewBinding.etContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String str = s.toString();
                if (str.length() > 10) {
                    str = str.substring(0, 10);
                    viewBinding.etContent.setText(str);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (editable.length() > 10) {
                    editable.delete(10, editable.length());
                }
            }
        });
        viewBinding.ivDel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                itemChildCLickListener.deletePos();
            }
        });
    }

    public interface ItemChildCLickListener {
        void deletePos();
    }

    public boolean getViewContent() {
        if (TextUtils.isEmpty(viewBinding.etTitle.getText()) && TextUtils.isEmpty(viewBinding.etContent.getText())) {
            return true;
        } else {
            return false;
        }
    }

    public UserInquiryForTender.CustomFieldListBean getContent() {
        UserInquiryForTender.CustomFieldListBean customFieldListBean = new UserInquiryForTender.CustomFieldListBean();
        customFieldListBean.setTitle(viewBinding.etTitle.getText().toString());
        customFieldListBean.setValue(viewBinding.etContent.getText().toString());
        return customFieldListBean;
    }

    public void setItemChildCLickListener(ItemChildCLickListener itemChildCLickListener) {
        this.itemChildCLickListener = itemChildCLickListener;
    }

}
